package com.achievo.know.web.faq;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.FaqApplication;
import com.achievo.know.domain.Faq;
import com.achievo.know.web.util.PageActionSupport;
import com.dayatang.querychannel.support.Page;
import com.redhat.redwork.coc.RedworkAction;

public class FaqAction extends PageActionSupport implements RedworkAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4453319559830309563L;

	Logger logger = LoggerFactory.getLogger(FaqAction.class);

	@Autowired
	private FaqApplication faqApplication;

	private String jsonResult;

	private String code;

	private Faq faq;

	private int from;

	private Long fkId;

	private String parent;

	private List<Faq> faqs;

	private Long faqId;

	private Page<Faq> list;

	private String ids;// 选择删除的IDS

	private String categoryId;

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public Page<Faq> getList() {
		return list;
	}

	public void setList(Page<Faq> list) {
		this.list = list;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Long getFaqId() {
		return faqId;
	}

	public void setFaqId(Long faqId) {
		this.faqId = faqId;
	}

	public List<Faq> getFaqs() {
		return faqs;
	}

	public void setFaqs(List<Faq> faqs) {
		this.faqs = faqs;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}

	public int getFrom() {
		return from;
	}

	public void setFrom(int from) {
		this.from = from;
	}

	public Long getFkId() {
		return fkId;
	}

	public void setFkId(Long fkId) {
		this.fkId = fkId;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Faq getFaq() {
		return faq;
	}

	public void setFaq(Faq faq) {
		this.faq = faq;
	}

	public void setFaqApplication(FaqApplication faqApplication) {
		this.faqApplication = faqApplication;
	}

	public String save() {
		try {
			faq.setPublishTime(new Date());
			faq.setUser(getUserRealName());
			faq.setFrom(from);
			faq.setFkId(fkId);
			if (StringUtils.isNotEmpty(categoryId))
				faq.setCategoryId(Long.parseLong(categoryId));
			/*
			 * if(StringUtils.isNotEmpty(parent)&&!"null".equals(parent)){ Faq
			 * p=faqApplication.get(Long.parseLong(parent)); faq.setParent(p); }
			 */
			faqApplication.save(faq);
			code = "1";
			jsonResult = "保存成功";
		} catch (Exception e) {
			logger.error(e.getMessage());
			code = "0";
			jsonResult = "保存出错：" + e.getMessage();
		}
		return JSON;
	}

	public String loadFaq() {
		list = faqApplication.getNewsFaq(fkId, currentPage, 5);
		return "load-faq";
	}

	public String loadFileFaq() {
		list = faqApplication.getFileFaq(fkId, currentPage, 5);
		return "load-faq";
	}

	public String loadFeedback() {
		return "feedback";
	}

	public String feedback() {
		try {
			Faq feedfor = faqApplication.get(faqId);
			if (feedfor != null) {
				Faq temp = new Faq();
				// String
				// content="//@"+feedfor.getUser()+":"+feedfor.getContent()+"<br>"+faq.getContent();
				temp.setContent(faq.getContent());
				temp.setUser(getUserRealName());
				temp.setPublishTime(new Date());
				temp.setFrom(feedfor.getFrom());
				temp.setFkId(feedfor.getFkId());
				temp.setParent(feedfor);
				temp.setCategoryId(feedfor.getCategoryId());
				faqApplication.save(temp);
				code = "1";
				jsonResult = "保存成功";
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			code = "0";
			jsonResult = "保存出错：" + e.getMessage();
		}
		return JSON;
	}

	public String home() {
		if (isSuperAdmin())
			list = faqApplication.queryPage(null, currentPage, pageSize);
		else
			list = faqApplication
					.queryPage(getAdminCateIds(), currentPage, pageSize);
		return "home";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delBatch() {
		try {
			if (StringUtils.isNotEmpty(ids)) {
				String[] idArr = ids.split(",");
				faqApplication.batchDelete(idArr);
				jsonResult = "删除成功!";
				code = "1";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			jsonResult = "出错：" + e.getMessage();
			code = "0";
		}
		return JSON;
	}

	public String Html2Text(String inputString) {
		String htmlStr = inputString; // 含html标签的字符串
		String textStr = "";
		java.util.regex.Pattern p_html;
		java.util.regex.Matcher m_html;

		try {
			String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); // 过滤html标签

			textStr = htmlStr;

		} catch (Exception e) {
			System.err.println("Html2Text: " + e.getMessage());
		}

		return textStr;// 返回文本字符串
	}
}
